﻿using System;
using System.Data;
using msBsc.AppLogic.DataAlc;

namespace msBsc.WebUi
{
    public partial class wfOrderFinder : msBsc.Cls.BPageHt
    {

        //CBizShpCommercialTenantInfo objShpCommercialTenantInfo = new CBizShpCommercialTenantInfo();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindControls();
                //System.Random rd = new Random();
                //sRadom.Value = this.GetRadomString();
            }
        }

        private void BindControls()
        {
            if (!IsPostBack)
            {
                //DataSet ds;
                //using (Epm.AppLogic.Framework.CDbAccess oCDbAccess = new Epm.AppLogic.Framework.CDbAccess())
                //{
                //    ds = oCDbAccess.QueryBySQL("SELECT FLOOR_ID,MAIN_GOODS_NME FROM SHP_GOODS_FLOOR");
                //    ds.Tables[0].TableName = "SHP_GOODS_FLOOR";

                //}
                //BoundComboBox("FLOOR_ID", ds, "MAIN_GOODS_NME", "FLOOR_ID", "", "", true);
            }
        }
        //重构基类的回调处理函数，以strAction属性来判断是什么类型的回调，该变量的值对应前台的ACTION参数，如：按钮的名称等
        public override void InitialByParam()
        {
            if (this.gtGridRet != null)
            {
                if (this.gtGridRet.action == "load" || this.gtGridRet.action == "export")//处理GRID的数据原获取和数据导出
                {
                    DataSet ds = GetDisplayData(this.GetParamHtValue("gtGridID"), this.strAction, this.sCurrPageRadom);
                    if (ds != null && ds.Tables.Count > 0)
                        SetReturnDatatable(ds.Tables[0], "ORDERS_INFO_ID");//CONSTANT_CATEGORY_ID为表的KEY字段，可填可不填
                }
                //处理数据保存 
            }
            else if (strAction == "btnEdit")//根据传入的回调类型进行处理，这里响应编辑按钮
            {
                SetMode("EDIT");
            }
            else if (strAction == "btnNotSave")//根据传入的回调类型进行处理，这里响应编辑按钮
            {
                SetMode("VIEW");
            }
            else if (strAction == "btnConfirm")//根据传入的回调类型进行处理，这里响应编辑按钮
            {
                ConfirmPaid(this.GetParamHtValue("CHECK_ID"));
                this.ExecJScript("btnDo('mFinder');");
            }
            else if (strAction == "btnCancel")//根据传入的回调类型进行处理，这里响应编辑按钮
            {
                CancelOrder(this.GetParamHtValue("CHECK_ID"));
                this.ExecJScript("btnDo('mFinder');");
            }

            base.InitialByParam();
        }

        private void ConfirmPaid(string strIds)
        {
            using (msBsc.AppLogic.DataAlc.CBizShpOrderForm objCBizShpOrderForm = new AppLogic.DataAlc.CBizShpOrderForm())
            {
                System.Transactions.TransactionOptions transactionOption = new System.Transactions.TransactionOptions();
                transactionOption.IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted;
                transactionOption.Timeout = new TimeSpan(0, 10, 0);
                using (System.Transactions.TransactionScope ts = new System.Transactions.TransactionScope(System.Transactions.TransactionScopeOption.Required, transactionOption))
                {
                    DataSet ds = objCBizShpOrderForm.SelectByIds(strIds);
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        if (ds.Tables[0].Rows[i]["TRADE_STATUS_TXT"].ToString() == "等待买家付款")
                        {
                            ds.Tables[0].Rows[i]["TRADE_STATUS_TXT"] = "买家已付款";
                        }
                        else
                        {
                            throw new Exception("订单处于" + ds.Tables[0].Rows[i]["TRADE_STATUS_TXT"].ToString() + "状态，不能确认付款！");
                        }
                    }
                    if (ds.HasChanges())
                    {
                        objCBizShpOrderForm.Update(ds);
                        ds.AcceptChanges();

                    }
                    ts.Complete();
                    ts.Dispose();
                }
            }
        }

        private void CancelOrder(string strIds)
        {
            using (msBsc.AppLogic.DataAlc.CBizShpOrderForm objCBizShpOrderForm = new AppLogic.DataAlc.CBizShpOrderForm())
            {
                System.Transactions.TransactionOptions transactionOption = new System.Transactions.TransactionOptions();
                transactionOption.IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted;
                transactionOption.Timeout = new TimeSpan(0, 10, 0);
                using (System.Transactions.TransactionScope ts = new System.Transactions.TransactionScope(System.Transactions.TransactionScopeOption.Required, transactionOption))
                {
                    DataSet ds = objCBizShpOrderForm.SelectByIds(strIds);
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        if (ds.Tables[0].Rows[i]["TRADE_STATUS_TXT"].ToString() == "等待买家付款")
                        {
                            ds.Tables[0].Rows[i]["TRADE_STATUS_TXT"] = "交易关闭";
                            ds.Tables[0].Rows[i]["ORDER_CLOSE_DESC"] = "商城管理人员取消订单";
                        }
                        else
                        {
                            throw new Exception("订单处于" + ds.Tables[0].Rows[i]["TRADE_STATUS_TXT"].ToString() + "状态，不能被取消！");
                        }
                    }
                    if (ds.HasChanges())
                    {
                        objCBizShpOrderForm.Update(ds);
                        ds.AcceptChanges();

                    }
                    ts.Complete();
                    ts.Dispose();
                }
            }
        }

        //修改页面状态
        private void SetMode(string strMode)
        {
            EditMode = strMode; switch (strMode)
            {
                case "EDIT":
                    EnabledLinkBtn("btnEdit", false);
                    EnabledLinkBtn("btnSave", true);
                    EnabledLinkBtn("btnNotSave", true);
                    EnabledLinkBtn("btnConfirm", false);
                    EnabledLinkBtn("btnCancel", false);
                    this.ExecJScript("doEditGt('myGrid1');");
                    break;
                case "ADD":
                    EnabledLinkBtn("btnEdit", false);
                    EnabledLinkBtn("btnSave", true);
                    EnabledLinkBtn("btnNotSave", false);
                    EnabledLinkBtn("btnConfirm", false);
                    EnabledLinkBtn("btnCancel", false);
                    this.ExecJScript("doEditGt('myGrid1');");
                    break;
                case "VIEW":
                    EnabledLinkBtn("btnEdit", true);
                    EnabledLinkBtn("btnSave", false);
                    EnabledLinkBtn("btnNotSave", false);
                    EnabledLinkBtn("btnConfirm", true);
                    EnabledLinkBtn("btnCancel", true);
                    this.ExecJScript("doCancelGt('myGrid1');");
                    break;
            }
        }
        //获取数据原,"DP_DATA"字符一定不要改变，否则系统不能自动清除缓存
        public DataSet GetDisplayData(string strgtGridID, string strSrcF, string strsRadom)
        {
            if (strgtGridID == "myGrid1")
            {
                DataSet ds;
                if (strSrcF == "mFinder" || !this.SessionIsExists(strsRadom + "DP_DATA", "1"))//点击查询按钮时，或需要数据而没有缓存时，根据条件取出数据记录
                {
                    using (msBsc.AppLogic.DataAlc.CBizOrdersInfo objCBizOrdersInfo = new AppLogic.DataAlc.CBizOrdersInfo())
                    {
                        string orderCode = this.GetParamHtValue("orderCode");
                        orderCode = string.IsNullOrEmpty(orderCode) ? "" : "%" + orderCode + "%";

                        string orderDesc = this.GetParamHtValue("orderDesc");
                        orderDesc = string.IsNullOrEmpty(orderDesc) ? "" : "%" + orderDesc + "%";

                        int orderStatus = string.IsNullOrEmpty(this.GetParamHtValue("orderStatus")) ? 0 : int.Parse(this.GetParamHtValue("orderStatus"));

                        string buyerNick = this.GetParamHtValue("buyerNick");
                        buyerNick = string.IsNullOrEmpty(buyerNick) ? "" : "%" + buyerNick + "%";

                        string orderTimeStart = this.GetParamHtValue("orderTimeStart");
                        

                        string orderTimeStop = this.GetParamHtValue("orderTimeStop");
                        


                        ds = objCBizOrdersInfo.GetOrdersInfoForShpView(orderCode, orderDesc, orderStatus, buyerNick, orderTimeStart, orderTimeStop);

                        if (ds != null & ds.Tables[0].Rows.Count > 0)
                        {

                            ds.Tables[0].Columns.Add(new DataColumn("ORDER_STATUS_NAME", typeof(string)));

                            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                            {
                                switch (Convert.ToInt32(ds.Tables[0].Rows[i]["ORDER_STATUS"]))
                                {
                                    case (int)OrderStatusEnum.ApplyPayBack:
                                        ds.Tables[0].Rows[i]["ORDER_STATUS_NAME"] = "申请退款"; break;
                                    case (int)OrderStatusEnum.CancelOrder:
                                        ds.Tables[0].Rows[i]["ORDER_STATUS_NAME"] = "订单已取消"; break;
                                    case (int)OrderStatusEnum.CloseStream:
                                        ds.Tables[0].Rows[i]["ORDER_STATUS_NAME"] = "流程已关闭"; break;
                                    case (int)OrderStatusEnum.DeleteOrder:
                                        ds.Tables[0].Rows[i]["ORDER_STATUS_NAME"] = "订单已删除"; break;
                                    case (int)OrderStatusEnum.Finished:
                                        ds.Tables[0].Rows[i]["ORDER_STATUS_NAME"] = "已完成"; break;
                                    case (int)OrderStatusEnum.PaiedAll:
                                        ds.Tables[0].Rows[i]["ORDER_STATUS_NAME"] = "已支付"; break;
                                    case (int)OrderStatusEnum.RefundCanceled:
                                        ds.Tables[0].Rows[i]["ORDER_STATUS_NAME"] = "取消退款"; break;
                                    case (int)OrderStatusEnum.Refusal:
                                        ds.Tables[0].Rows[i]["ORDER_STATUS_NAME"] = "拒绝退款"; break;
                                    case (int)OrderStatusEnum.RefuseOrder:
                                        ds.Tables[0].Rows[i]["ORDER_STATUS_NAME"] = "拒绝订单"; break;
                                    case (int)OrderStatusEnum.SolderChecked:
                                        ds.Tables[0].Rows[i]["ORDER_STATUS_NAME"] = "商家已确认"; break;
                                    case (int)OrderStatusEnum.SurePayBack:
                                        ds.Tables[0].Rows[i]["ORDER_STATUS_NAME"] = "确认退款"; break;
                                    case (int)OrderStatusEnum.WaitingCheck:
                                        ds.Tables[0].Rows[i]["ORDER_STATUS_NAME"] = "等待确认"; break;
                                }
                            }
                        }

                        ds.AcceptChanges();
                    }
                    SetSession(strsRadom + "DP_DATA", ds, "1");
                    //查询时，将页面置为查看状态
                    if (strSrcF == "mFinder")
                        SetMode("VIEW");
                }
                else
                {
                    ds = (DataSet)this.GetSession(strsRadom + "DP_DATA", "1");
                }
                return ds;
            }
            else
            {
                return null;
            }
        }

    }
}